package com.qianfeng.biquge.utils;

import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;

public class MD5 {

    public static Map<String, String> enMD5(String passWord){

        String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        char[] chars = str.toCharArray();

        Random random=new Random();

        StringBuffer stringBuffer=new StringBuffer();

        for (int j = 0; j <7 ; j++) {

            int i = random.nextInt(str.length());

            stringBuffer.append(chars[i]);

        }

        String s = String.valueOf(System.currentTimeMillis());

        String substring = s.substring(s.length() - 3);

        stringBuffer.append(substring);



        String passWordAndSalt = passWord+ stringBuffer.toString();


        String md5 = DigestUtils.md5DigestAsHex(passWordAndSalt.getBytes(StandardCharsets.UTF_8));

        Map<String,String> map = new ConcurrentHashMap();

        map.put("passWordSalt",stringBuffer.toString());
        map.put("md5",md5);

        return map;

    }

    public static boolean deMD5(String passWord,String passWordSalt,String md5){

        String passWordAndSalt =passWord + passWordSalt;

        String md51 = DigestUtils.md5DigestAsHex(passWordAndSalt.getBytes(StandardCharsets.UTF_8));

        if (md5.equals(md51)){

            return true;

        }

        return false;

    }


}
